﻿#include "DesignBLL.h"
#include <QDebug>

DesignBLL::DesignBLL(int keyMode, QObject *parent) : QAbstractListModel(parent), m_keyMode(keyMode)
{
    if (m_keyMode == 0)
    {
        dataList.append(new DesignModel(false, m_keyMode, "F", "F", "Q 2 ", "1000"));
    }
    else if (m_keyMode == 1)
    {
        dataList.append(new DesignModel(false, m_keyMode, "H", "5", "", "500"));
    }
    else if (m_keyMode == 2)
    {
        dataList.append(new DesignModel(false, m_keyMode, "N", "", "", "400"));
    }

    for (int i = 0; i < dataList.count(); i++)
    {
        DesignModel *model = qobject_cast<DesignModel *>(dataList[i]);
        monitorDataListChanged(model);
    }
}

void DesignBLL::monitorDataListChanged(DesignModel *model)
{
    connect(model, &DesignModel::enableChanged, [=]() {
        onDataListChanged(model);
    });

    QObject::connect(model, &DesignModel::startKeyChanged, [=]() {
        onDataListChanged(model);
    });

    QObject::connect(model, &DesignModel::stopKeyChanged, [=]() {
        onDataListChanged(model);
    });

    QObject::connect(model, &DesignModel::sequenceKeyChanged, [=]() {
        onDataListChanged(model);
    });

    QObject::connect(model, &DesignModel::intervalChanged, [=]() {
        onDataListChanged(model);
    });
}

int DesignBLL::rowCount(const QModelIndex &parent) const
{
    //    qInfo() << "dataList.count:" << dataList.count();
    Q_UNUSED(parent);
    return dataList.count();
}

QVariant DesignBLL::data(const QModelIndex &index, int role) const
{
    //    qInfo() << "role:" << role;
    DesignModel *model = qobject_cast<DesignModel *>(dataList[index.row()]);
    switch (role)
    {
    case 1:
        return model->enable();
        break;
    case 2:
        return model->startKey();
        break;
    case 3:
        return model->stopKey();
        break;
    case 4:
        return model->sequenceKey();
        break;
    case 5:
        return model->interval();
        break;
    default:
        break;
    }
    return QVariant();
}

bool DesignBLL::setData(const QModelIndex &index, const QVariant &value, int role)
{
    DesignModel *model = qobject_cast<DesignModel *>(dataList[index.row()]);
    if (index.row() >= 0 && index.row() < dataList.count())
    {
        switch (role)
        {
        case 1:
            model->setEnable(value.value<bool>());
            break;
        case 2:
            model->setStartKey(value.value<QString>());
            break;
        case 3:
            model->setStopKey(value.value<QString>());
            break;
        case 4:
            model->setSequenceKey(value.value<QString>());
            break;
        case 5:
            model->setInterval(value.value<QString>());
            break;
        default:
            break;
        }
        return true;
    }
    return false;
}

QHash<int, QByteArray> DesignBLL::roleNames() const
{
    //    qInfo() << "roleNames";
    QHash<int, QByteArray> d;
    d[1] = "enable";
    d[2] = "startKey";
    d[3] = "stopKey";
    d[4] = "sequenceKey";
    d[5] = "interval";
    return d;
}

void DesignBLL::onDataListChanged(DesignModel *model)
{
#ifdef Q_OS_WIN32
    KeyCapturer::instance()->saveKeyCapturer();
#endif

    if (!model->enable() && model->running())
    {
        model->setRunning(false);
        model->mThreadObject->stop();
        qInfo() << "switch disenable";
    }
    qInfo() << model->enable() << model->startKey() << model->stopKey() << model->sequenceKey() << model->interval();
}

void DesignBLL::append()
{
    //    qInfo() << "append";
    int index = dataList.count();
    beginInsertRows(QModelIndex(), index, index);
    DesignModel *model = new DesignModel(false, m_keyMode, "", "", "", "1000");
    dataList.append(model);
    monitorDataListChanged(model);
    endInsertRows();
}

void DesignBLL::remove(int index)
{
    //    qInfo() << "remove index = " << index;
    beginRemoveRows(QModelIndex(), index, index);
    DesignModel *model = qobject_cast<DesignModel *>(dataList[index]);
    delete model;
    dataList.removeAt(index);
    endRemoveRows();
}
